﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
   public  class SearchCountDAO
    {
       CameraDBEntities db = new CameraDBEntities();
        public List<SearchCount> GetListAll()
        {
            return db.SearchCounts.ToList();
        }
        public List<SearchCount> GetListAll_Sort()
        {
            return db.SearchCounts.OrderByDescending(x => x.idSearch).ToList();
        }
        public List<SearchCount> GetListTop10()
        {
            return db.SearchCounts.OrderByDescending(x => x.idSearch).Take(10).ToList();
        }
        public List<SearchCount> GetListCountTop10()
        {
            return db.SearchCounts.OrderByDescending(x => x.count).Take(10).ToList();
        }
        public SearchCount GetById(int id)
        {
            return db.SearchCounts.Where(x => x.idSearch == id).SingleOrDefault();
        }
        public SearchCount GetByKeys(string id)
        {
            return db.SearchCounts.Where(x => x.keys == id).SingleOrDefault();
        }

        public bool Insert(SearchCount c)
        {
            try
            {
                db.SearchCounts.Add(c);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Edit(SearchCount ca)
        {
            try
            {
                SearchCount a = new SearchCount();
                a = db.SearchCounts.Where(x => x.idSearch == ca.idSearch).SingleOrDefault();
                a.keys = ca.keys;
                a.count = ca.count; 
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Edit_Count(string keys)
        {
            try
            {
                SearchCount a = new SearchCount();
                a = db.SearchCounts.Where(x => x.keys ==keys).SingleOrDefault();
                a.count = a.count + 1;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                SearchCount a = new SearchCount();
                a = db.SearchCounts.Where(x => x.idSearch == id).SingleOrDefault();
                db.SearchCounts.Remove(a);
                 
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
